﻿@model QuanLySanXuat.Models.OrderFormModels
@{
    ViewBag.Title = "Lệnh sản xuất";
}
<link href="@Url.Content("~/ContentTMT/dateui.css")" rel="stylesheet" />
<script type="text/javascript">
    $(document).ready(function () {
        $(function () {
            $("#Date1").datepicker();
            $("#Date2").datepicker();
            $("#EndDate").datepicker();
            $("#StartDate").datepicker();
            $("#Date1").datepicker("option", "dateFormat", "dd/mm/yy");
            $("#Date2").datepicker("option", "dateFormat", "dd/mm/yy");
            $("#EndDate").datepicker("option", "dateFormat", "dd/mm/yy");
            $("#StartDate").datepicker("option", "dateFormat", "dd/mm/yy");
        });
    });
</script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#CheckValid').bootstrapValidator({
            message: 'This value is not valid',
            excluded: [':disabled'],
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                Code: {
                    validators: {
                        notEmpty: {
                            message: 'Mã không để trống'
                        }
                    }
                },
                User1ID: {
                    validators: {
                        notEmpty: {
                            message: 'Phải chọn người lập phiếu'
                        }
                    }
                },
                User2ID: {
                    validators: {
                        notEmpty: {
                            message: 'Phải chọn người duyệt phiếu'
                        }
                    }
                },
                ProductModelID: {
                    validators: {
                        notEmpty: {
                            message: 'Phải chọn mã sản phẩm'
                        }
                    }
                },
            }
        }).on('success.form.bv', function (e) {
            // Prevent form submission
            e.preventDefault();

            // Get the form instance
            var $form = $(e.target);

            // Get the BootstrapValidator instance
            var bv = $form.data('bootstrapValidator');
            $("#Date1").datepicker("option", "dateFormat", "mm/dd/yy");
            $("#Date2").datepicker("option", "dateFormat", "mm/dd/yy");
            $("#StartDate").datepicker("option", "dateFormat", "mm/dd/yy");
            $("#EndDate").datepicker("option", "dateFormat", "mm/dd/yy");
            // Use Ajax to submit form data
            $.ajax({
                type: "POST",
                url: '@Url.Action("Save", "Order")',
                async: false,
                cache: false,
                data: $('#CheckValid').serialize(),
                success: function (result) {
                    location.href = '@Url.Action("Index", "Order")'
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(xhr.responseText);
                    alert(thrownError);
                }
            });
        });
    });
</script>
<script type="text/javascript">
    function AddDesign(element) {
        UpdateDesignDetail(element);
    }

    function SetFile(element) {
        var tbody = $(element).closest('tr');
        var finder = new CKFinder();
        finder.selectActionFunction = function (fileUrl) {
            $('.desginFile', tbody).val(fileUrl);
        };
        finder.popup();
    }

    function UpdateDesignDetail(element) {
        var tbody = $(element).closest('tr');
        var code = $(".code", tbody).val();
        var name = $(".name", tbody).val();
        var des = $(".description", tbody).val();
        var type = $(".type", tbody).val();
        var dfile = $(".desginFile", tbody).val();
        var designID = $(".designID", tbody).val();
        var productID = $("#ID").val();

        if (code == "" || name == "" || type == "") {
            alert("Bạn chưa nhập đủ thông tin");
        }
        else {
            $.ajax({
                type: "POST",
                url: '@Url.Action("UpdateDesignDetail", "Order")',
                async: false,
                cache: false,
                data: { ID: designID, Code: code, Name: name, Description: des, DesignFile: dfile, Type: type, ProductID: productID },
                success: function (result) {
                    $('.designID', tbody).val(result);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(xhr.responseText);
                    alert(thrownError);
                }
            });
        }
        GetDesignDetail($('.designID', tbody).val(), $("#ID").val());
    }
</script>
<div id="breadcrumb">
    <div class="container">
        <div id="con_top">
            <div class="c_container">
                <h3>Lệnh sản xuất</h3>
            </div>
        </div>
    </div>
</div>
<form class="form-horizontal" role="form" id="CheckValid" style="margin-top: 10px;">
    @if (Model == null)
    {
    
        <div class="col-sm-6">
            <div class="form-group">
                <label for="Code" class="col-sm-4 control-label">Mã bản ghi (*)</label>
                <div class="col-sm-8">
                    <input type="hidden" id="ID" name="ID" />
                    <input type="text" class="form-control" id="Code" name="Code" placeholder="Code">
                </div>
            </div>
            <div class="form-group">
                <label for="Code" class="col-sm-4 control-label">Người lập phiếu (*)</label>
                <div class="col-sm-8">
                    @Html.DropDownList("User1ID", new SelectList(ViewData["Employee"] as IEnumerable<Data.Models.Employee>, "ID", "Name"), "Chọn người lập phiếu", new { @class = "form-control" })
                </div>
            </div>
            <div class="form-group">
                <label for="dtEfective" class="col-sm-4 control-label">Ngày lập phiếu</label>
                <div class="col-sm-8">
                    <div class='input-group date'>
                        <input type="text" class="form-control" id="Date1" name="Date1">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="Code" class="col-sm-4 control-label">Người duyệt phiếu (*)</label>
                <div class="col-sm-8">
                    @Html.DropDownList("User2ID", new SelectList(ViewData["Employee"] as IEnumerable<Data.Models.Employee>, "ID", "Name"), "Chọn người duyệt phiếu", new { @class = "form-control" })
                </div>
            </div>
            <div class="form-group">
                <label for="dtEfective" class="col-sm-4 control-label">Ngày duyệt phiếu</label>
                <div class="col-sm-8">
                    <div class='input-group date'>
                        <input type="text" class="form-control" id="Date2" name="Date2">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="dtEfective" class="col-sm-4 control-label">Ngày bắt đầu</label>
                <div class="col-sm-8">
                    <div class='input-group date'>
                        <input type="text" class="form-control" id="StartDate" name="StartDate">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-6">

            <div class="form-group">
                <label for="dtEfective" class="col-sm-3 control-label">Ngày hoàn thành</label>
                <div class="col-sm-8">
                    <div class='input-group date'>
                        <input type="text" class="form-control" id="EndDate" name="EndDate">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="Code" class="col-sm-3 control-label">Mã sản phẩm(*)</label>
                <div class="col-sm-8">
                    @Html.DropDownList("ProductModelID", new SelectList(ViewData["ProductModel"] as IEnumerable<Data.Models.ProductModel>, "ID", "Name"), "Chọn mã sản phẩm", new { @class = "form-control" })
                </div>
            </div>
            <div class="form-group">
                <label for="dtEfective" class="col-sm-3 control-label">Số lượng</label>
                <div class="col-sm-8">
                    <div class='input-group date'>
                        <input type="text" class="form-control" id="Quantity" name="Quantity">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="Description" class="col-sm-3 control-label">Ghi chú</label>
                <div class="col-sm-8">
                    <textarea id="Note" name="Note" class="form-control" rows="5" placeholder="Mô tả"></textarea>
                </div>
            </div>
        </div>
    }
    else
    {
        <div class="col-sm-6">
            <div class="form-group">
                <label for="Code" class="col-sm-4 control-label">Mã bản ghi (*)</label>
                <div class="col-sm-8">
                    <input type="hidden" id="ID" name="ID" value="@Model.ID"  />
                    <input type="text" class="form-control" id="Code" name="Code" placeholder="Code" value="@Model.Code">
                </div>
            </div>
            <div class="form-group">
                <label for="Code" class="col-sm-4 control-label">Người lập phiếu (*)</label>
                <div class="col-sm-8">
                    @Html.DropDownList("User1ID", new SelectList(ViewData["Employee"] as IEnumerable<Data.Models.Employee>, "ID", "Name", @Model.User1ID), "Chọn người lập phiếu", new { @class = "form-control" })
                </div>
            </div>
            <div class="form-group">
                <label for="dtEfective" class="col-sm-4 control-label">Ngày lập phiếu</label>
                <div class="col-sm-8">
                    <div class='input-group date'>
                        <input type="text" class="form-control" id="Date1" name="Date1" value="@Convert.ToDateTime(Model.Date1).ToShortDateString()">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="Code" class="col-sm-4 control-label">Người duyệt phiếu (*)</label>
                <div class="col-sm-8">
                    @Html.DropDownList("User2ID", new SelectList(ViewData["Employee"] as IEnumerable<Data.Models.Employee>, "ID", "Name", Model.User2ID), "Chọn người duyệt phiếu", new { @class = "form-control" })
                </div>
            </div>
            <div class="form-group">
                <label for="dtEfective" class="col-sm-4 control-label">Ngày duyệt phiếu</label>
                <div class="col-sm-8">
                    <div class='input-group date'>
                        <input type="text" class="form-control" id="Date2" name="Date2" value="@Convert.ToDateTime(Model.Date2).ToShortDateString()" >
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="dtEfective" class="col-sm-4 control-label">Ngày bắt đầu</label>
                <div class="col-sm-8">
                    <div class='input-group date'>
                        <input type="text" class="form-control" id="StartDate" name="StartDate" value="@Convert.ToDateTime(Model.StartDate).ToShortDateString()" >
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-6">

            <div class="form-group">
                <label for="dtEfective" class="col-sm-3 control-label">Ngày hoàn thành</label>
                <div class="col-sm-8">
                    <div class='input-group date'>
                        <input type="text" class="form-control" id="EndDate" name="EndDate" value="@Convert.ToDateTime(Model.EndDate).ToShortDateString()" >
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="Code" class="col-sm-3 control-label">Mã sản phẩm(*)</label>
                <div class="col-sm-8">
                    @Html.DropDownList("ProductModelID", new SelectList(ViewData["ProductModel"] as IEnumerable<Data.Models.ProductModel>, "ID", "Name", Model.ProductModelID), "Chọn mã sản phẩm", new { @class = "form-control" })
                </div>
            </div>
            <div class="form-group">
                <label for="dtEfective" class="col-sm-3 control-label">Số lượng</label>
                <div class="col-sm-8">
                    <div class='input-group date'>
                        <input type="text" class="form-control" id="Quantity" name="Quantity" value="@Model.Quantity" >
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="Description" class="col-sm-3 control-label">Ghi chú</label>
                <div class="col-sm-8">
                    <textarea id="Note" name="Note" class="form-control" rows="5" placeholder="Mô tả">@Model.Note</textarea>
                </div>
            </div>
        </div>
    }
    <div class="form-group">
        <div class="c_container col-sm-10 col-sm-offset-1" id="OrderDetail">
            @{Html.RenderPartial("_OrderDetails", Model);}
        </div>
    </div>
</form>
<script type="text/javascript">
    var $frtDesgin;
    var $frtMaterial;
    var $frtTool;
    var $frtOther;
    var $frtLabour;
    function RemoveRow(element, tbl) {
        var rowCount = $('#' + tbl + ' tbody tr').length;
        if (rowCount < 1) {
            return;
        }
        else {

            var tr = $(element).closest('tr');
            if (tbl == 'tblDesgin') {
                var designID = $(".designID", tr).val();
                if (designID != 0) {
                    $.ajax({
                        type: "POST",
                        url: '@Url.Action("Remove", "Order")',
                                async: false,
                                cache: false,
                                data: { ID: designID },
                                success: function (result) {
                                    //Xoa dong cac bang co type:designID
                                    //RemoveAllRow(designID);
                                },
                                error: function (xhr, ajaxOptions, thrownError) {
                                    alert(xhr.status);
                                    alert(xhr.responseText);
                                    alert(thrownError);
                                }
                            });
                            GetDesignDetail(designID, $("#ID").val());
                        }
                    }
                    else {

                        var code = $(".code", tr).val();
                        var designID = $(".desgingchange", tr).val();

                        if (designID == "") {
                            designID = 0;
                        }
                        if (code != null) {
                            $.ajax({
                                type: "Get",
                                url: '@Url.Action("RemoveDesign", "Order")',
                        async: false,
                        cache: false,
                        data: { Code: code},
                        success: function (result) {

                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert(xhr.status);
                            alert(xhr.responseText);
                            alert(thrownError);
                        }
                    });
                }

            }
            tr.remove();
        }
    };

    function GetPrice(element) {
        var tr = $(element).closest('tr');
        var priceID = $(".billchange", tr).val();
        var materialID = $(".materialID", tr).val();
        $.ajax({
            type: "Get",
            url: '@Url.Action("GetPrice", "Order")',
                                async: false,
                                cache: false,
                                data: { Code: priceID, MaterialID: materialID },
                                success: function (result) {
                                    $(".pricematerial",tr).val(result);
                                },
                                error: function (xhr, ajaxOptions, thrownError) {
                                    alert(xhr.status);
                                    alert(xhr.responseText);
                                    alert(thrownError);
                                }
        });
    };
    function GetNorm(element, tbl) {
        var tr = $(element).closest('tr');
        var code = $(".norm", tr).val();
        $.ajax({
            type: "Get",
            url: '@Url.Action("GetNorm", "Order")',
            async: false,
            cache: false,
            data: { Code: code, Name: tbl},
            success: function (result) {
                $(".unit",tr).val(result.Unit);
                $(".unitNorm",tr).val(result.Norm);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(xhr.responseText);
                alert(thrownError);
            }
        });
    };
    $(document).ready(function () {
        $frtDesgin = "<tr><td><input type='hidden' id='designID' name='designID' value='0' class='designID'><input type='text' id='CodeDesgin' name='CodeDesgin' class='txt1 code'></td><td><input type='text' id='NameDesign' name='NameDesign' class='txt1 name'></td><td><select class='span2 col-sm-1 form-control type' id='TypeID' name='TypeID'><option value=''>Chọn kiểu</option><option value='0'>Toàn bộ</option><option value='1'>Chi tiết</option></select></td><td><input type='text' id='DescriptionDesign' name='DescriptionDesign' class='txt1 description'></td><td><input type='text' id='DesignFile' name='DesignFile' class='txt1 desginFile' onclick='SetFile(this);'></td><td><div class='remove' id='delRow' onclick='RemoveRow(this,\"tblDesgin\");'></div></td></tr>";
        $frtMaterial = jQuery('div#form_body table#tblMaterial').find("tbody tr:first").html();
        $frtLabour = jQuery('div#form_body table#tblLabour').find("tbody tr:first").html();
        $frtTool = jQuery('div#form_body table#tblTool').find("tbody tr:first").html();
        $frtOther = jQuery('div#form_body table#Other').find("tbody tr:first").html();
        $(".materialchange").change(function () {
            ChangeProduct(this);
        });
        $("#ProductModelID").change(function () {
            if (this.value != "") {
                GetDetail(this.value);
            }
        });
    });
    function GetDetail(element) {
        $.ajax({
            type: "GET",
            url: '@Url.Action("GetDetail", "Order")',
            async: false,
            cache: false,
            data: { ID: element },
            success: function (result) {
                $("#OrderDetail").html(result);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(xhr.responseText);
                alert(thrownError);
            }
        });
    }
    function GetDesignDetail(element, productID) {
        $.ajax({
            type: "GET",
            url: '@Url.Action("GetDesignDetail", "Order")',
            async: false,
            cache: false,
            data: { ID: element, ProductID: productID },
            success: function (result) {
                if (result == true) {
                    //Dung cho material
                    $("#MaterialDesignDetailID").get(0).options.length = 0;
                    $("#MaterialDesignDetailID").get(0).options[0] = new Option("Chọn chi tiết thiết kế", "-1");
                    //Dung cho labour
                    $("#LabourDesignDetailID").get(0).options.length = 0;
                    $("#LabourDesignDetailID").get(0).options[0] = new Option("Chọn chi tiết thiết kế", "-1");
                    //Dung cho Tool
                    $("#ToolDesignDetailID").get(0).options.length = 0;
                    $("#ToolDesignDetailID").get(0).options[0] = new Option("Chọn chi tiết thiết kế", "-1");
                    //Dung cho Other
                    $("#OtherDesignDetailID").get(0).options.length = 0;
                    $("#OtherDesignDetailID").get(0).options[0] = new Option("Chọn chi tiết thiết kế", "-1");
                }
                else {
                    //Dung cho material
                    $("#MaterialDesignDetailID").get(0).options.length = 0;
                    $("#MaterialDesignDetailID").get(0).options[0] = new Option("Chọn chi tiết thiết kế", "-1");
                    $.each(result, function (idx, obj) {
                        $("#MaterialDesignDetailID").get(0).options[$("#MaterialDesignDetailID").get(0).options.length] = new Option(obj.Code, obj.ID);
                    });
                    //Dung cho labour
                    $("#LabourDesignDetailID").get(0).options.length = 0;
                    $("#LabourDesignDetailID").get(0).options[0] = new Option("Chọn chi tiết thiết kế", "-1");
                    $.each(result, function (idx, obj) {
                        $("#LabourDesignDetailID").get(0).options[$("#LabourDesignDetailID").get(0).options.length] = new Option(obj.Code, obj.ID);
                    });
                    //Dung cho Tool
                    $("#ToolDesignDetailID").get(0).options.length = 0;
                    $("#ToolDesignDetailID").get(0).options[0] = new Option("Chọn chi tiết thiết kế", "-1");
                    $.each(result, function (idx, obj) {
                        $("#ToolDesignDetailID").get(0).options[$("#ToolDesignDetailID").get(0).options.length] = new Option(obj.Code, obj.ID);
                    });
                    //Dung cho Other
                    $("#OtherDesignDetailID").get(0).options.length = 0;
                    $("#OtherDesignDetailID").get(0).options[0] = new Option("Chọn chi tiết thiết kế", "-1");
                    $.each(result, function (idx, obj) {
                        $("#OtherDesignDetailID").get(0).options[$("#OtherDesignDetailID").get(0).options.length] = new Option(obj.Code, obj.ID);
                    });
                }

            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(xhr.responseText);
                alert(thrownError);
            }
        });
    }
</script>
